package com.example.baseutils.application

import android.app.Application
import android.os.Handler
import android.os.Looper
import android.os.Process
import com.example.baseutils.AppUtils
import com.example.baseutils.CommUtils
import com.example.baseutils.LogUtils
import com.example.baseutils.net.NetState

/**
 * Author by lp,on 2021/7/14/014,14:03.
 */
open class BaseApplication : Application() {
    companion object {
        @JvmStatic
        lateinit var instance: Application
    }

    override fun onCreate() {
        super.onCreate()
        val curProcess = AppUtils.getProcessName(this, Process.myPid())
        LogUtils.log("MyApplication", "进程 $curProcess")
        if (packageName != curProcess) {
            return
        }
        instance = this
        NetState.getInstance().init(instance)
        modulesApplicationInit()
        CommUtils.init(this, null, Thread.currentThread().id.toInt())
    }

    //通过反射方法找到对应的类，并调用接口
    private fun modulesApplicationInit() {
        for (moduleImpl in ModelConfig.MODULESLIST) {
            try {
                val clazz = Class.forName(moduleImpl)
                val `object` = clazz.newInstance()
                if (`object` is ApplicationImpl) {
                    `object`.onCreate(this)
                }
            } catch (e: ClassNotFoundException) {
                e.printStackTrace()
            } catch (e: IllegalAccessException) {
                e.printStackTrace()
            } catch (e: InstantiationException) {
                e.printStackTrace()
            }
        }
    }
}